/*
 * Copyright (c) 2011-2012 www.iflytek.com. All Rights Reserved.
 *  This software for customer relationship management system, developed by Iflytek-ZY-GCZX team.
 *  Software code and design for the team, copy rights reserved.
 */

var dashboard = function () {

	var priopitys = {};
	cdms.common.ajax_sync_get(system_vars.basePath + 'action/pripitys', function (data) {
		priopitys = eval(data);
	});
	/**
	 * 问题号格式化
	 * @type {string}
	 */
	var key_format = '<a href="{0}issue/view/{1}">{1}</a>';
	/**
	 * 标题格式化
	 * @type {string}
	 */
	var summary_format = '<a href="{0}issue/view/{1}">{2}</a>';

	var priopity_format = '<a href="{0}issue/view/{1}"><img src="{0}resources/common/images/{2}" class="icon" title="{3}" alt="{4}" height="13" width="14" border="0"/>{3}</a>';
	/** 
	 * 项目链接格式化
	 */
	var project_format = '<a href="{0}project/p_dashboard/{3}" title="{2}">{1}</a>';
	/**
	 * 问题号格式化
	 * @param item  数据
	 * @return {String} 格式化后的html文本
	 */
	var key_forrmart = function (item) {
		return key_format.format(system_vars.basePath, item.pkey);
	};
	var project_key_format = function(item){
		return project_format.format(system_vars.basePath,(item.name.length>15) ? (item.name.substr(0,14)+"...") : item.name,item.name,item.code);
	}
	
	var priopity_forrmat = function (item) {
		var priopity = {};

		$.each(priopitys, function (idx, val) {

			if (val.id == item.priopity) {
				priopity = val;
				return false;
			}
		});
		return  priopity_format.format(system_vars.basePath, item.pkey, priopity.iconUrl, priopity.description, priopity.name);
	};
	/**
	 * 问题标题格式化
	 * @param item  数据
	 * @return {String}  格式化后的html文本
	 */
	var title_format = function (item) {
		return summary_format.format(system_vars.basePath, item.pkey, item.summary);
	};
	/**
	 * 获取待办事项的总数
	 */
	var todo_count = function () {
		cdms.common.ajax_get('todo_count', function (data) {
			$('.todo_count').html(data);
		});
	};


	/**
	 * 获取当前人已报告的数据
	 */
	var report_count = function () {
		cdms.common.ajax_get('report_count', function (data) {
			$('.reporter_count').html(data);
		});
	};

	/**
	 * 表格显示信息
	 * @type {Array} 表格字段说明
	 */
	var grid_columns = [
		{title: "key", field: "pkey", width: "120px", headAlign: 'left', formatter: key_forrmart},
		{title: language.issue.column_1, field: "summary", headAlign: 'left', formatter: title_format},
		{title: language.issue.column_6, field: "priopity", width: "60px", headAlign: 'left', align: "left", formatter: priopity_forrmat},
		{title: language.issue.column_4, field: "created", width: "150px", headAlign: 'left'}
	];
	
	/**
	 * 表格显示项目信息
	 * @type {Array} 表格字段说明
	 */
	var project_grid_columns=[
	     {title : language.common.peoject_name,field : "name", width: "205px", headAlign: 'left', formatter: project_key_format}                     
    ];
	/**
	 * 设置指派给我的待办事项
	 */
	var todo_issue_me = function () {
		dashboard_grid('assigin_me', 'assign_grid');
	};
	/**
	 * 设置我报告的问题选项
	 */
	var report_issue_me = function () {
		dashboard_grid('report_me', 'report_grid');
	};
	
	/**
	 * 设置我的项目选项
	 */
	var project_grid =function(){
		project_grid_constuction('my_projects', 'project_grid');
	}
	/**
	 * 构建报告表格
	 * @param url  地址
	 * @param grid_id   表格ID
	 */
	var dashboard_grid = function (url, grid_id) {
		cdms.common.ajax_get(system_vars.basePath + url, function (data) {
			cdms.common.jart_grid(grid_id, {
				dataSource: data, showCheckBox: false, pageMode: 'disabled', idFields: 'id', columns: [grid_columns], emptyText: '暂无'
			});
		});

	};
	/**
	 * 构建项目表格
	 * @param url  地址
	 * @param grid_id   表格ID
	 */
	var project_grid_constuction = function (url, grid_id) {
		cdms.common.ajax_get(system_vars.basePath + url, function (data) {
			cdms.common.jart_grid(grid_id, {
				dataSource: data, showCheckBox: false, pageMode: 'disabled',showHead: false,columns: [project_grid_columns], emptyText: '暂无'
			});
		});

	};
	return {
		load_view: function () {
			todo_count();
			report_count();
			todo_issue_me();
			report_issue_me();
			project_grid();
		}
	}
}();

$(document).ready(function () {
	dashboard.load_view();
});